package com.jingdong.exception;


import com.jingdong.utils.ResultVOUtil;
import com.jingdong.vo.ResultVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局异常处理
 */
@ControllerAdvice
public class ExceptionHandle {
    private static final Logger logger = LoggerFactory.getLogger(ExceptionHandle.class);

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ResultVO handle(Exception e){
        if (e instanceof MyException) {
            MyException exception = (MyException)e;
            return ResultVOUtil.error(exception.getCode(),exception.getMessage());
        }else if (e instanceof org.springframework.web.servlet.NoHandlerFoundException){
            //404
            return ResultVOUtil.error(-1,"404 找不到！");
        }
        logger.error("异常：{}",e);
        return ResultVOUtil.error(-1,"未知错误");
    }

}
